package org.softmed.ATComm.server;

import java.util.List;

import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.softmed.ATComm.ATControlFacade;


/**
 * Resource which has only one representation.
 * 
 */
public class SerialPortListerRestlet extends Restlet {

	private ATControlFacade atControl;

	public SerialPortListerRestlet(ATControlFacade atControl) {
		this.atControl = atControl;
	}

	@Override
	public void handle(Request request, Response response) {
		try {
			String methodName = request.getMethod().getName();
			if (methodName.equals("GET")) {
				String ports = "";
				List<String> list = atControl.getSerialPorts();
				for (String string : list) {
					ports += string + "\n";
				}
				response.setEntity(ports, MediaType.TEXT_PLAIN);
				return;
			} else {
				response.setEntity("ERROR UNSUPPORTED HTTP METHOD "
						+ methodName, MediaType.TEXT_PLAIN);
				return;
			}

		} catch (Throwable t) {
			t.printStackTrace();
			response.setEntity("EXCEPTION ERROR", MediaType.TEXT_PLAIN);
			return;
		}

	}
}
